<?php

namespace Beerus\Modules\DesignerCompetition2020\Actions;

use Doraemon\model\DesignerCompetition\DesignerAward;
use Doraemon\service\admin\EnterAward;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;

class EditAward extends RowAction
{
    public $name = '奖项查看/录入';

    public function handle($signId, Request $request)
    {
        $award_ids = $request->post('award_ids');
        // 将奖项录入
        $rs = EnterAward::enter($signId, $award_ids);
        if ($rs == EnterAward::STATUS_LIMIT) {
            return $this->response()->error('有奖项已经超过最大数量');
        }
        if ($rs == EnterAward::STATUS_FAIL) {
            return $this->response()->error('作品奖项录取失败');
        }
        return $this->response()->success('奖项录取成功')->refresh();
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form()
    {
        // 获取所有可选奖项
        $awards = EnterAward::getAvailAward(2020);

        $awardIds = explode(',', $this->row->award_ids);
        $label = '报名id-' . $this->row->id . '获得奖项';
        $this->checkbox('award_ids', $label)
            ->options($awards)->default($awardIds);
    }
}
